package com.salesys.gr.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.salesys.gr.util.DbUtil;
import com.salesys.tony.domain.Record;

public class RecordDao {
	// 新增积分纪录
	public int addRecord(int userId, String mark, String markSource, String time) {
		String sql = "insert into `sale_record` (`userid`,`mark`,`marksource`,`time`) values (?, ?, ?, ?)";
		DbUtil.getConnection();
		DbUtil.setPerpareStatement(sql);
		DbUtil.setPrepareStatementString(1, userId + "");
		DbUtil.setPrepareStatementString(2, mark);
		DbUtil.setPrepareStatementString(3, markSource);
		DbUtil.setPrepareStatementString(4, time);
		DbUtil.updatePrepareStatement();
		int affectRow = DbUtil.getAffectRow();
		DbUtil.close();
		return affectRow;
	}

	// 从积分用户ID查询积分纪录
	public List<Record> queryMarkRecordFromUserid(String userId) {
		List<Record> recordList = new ArrayList<Record>();
		String sql = "select `mark`,`marksource`,`time` from `sale_record` where `userid`=?";
		DbUtil.getConnection();
		DbUtil.setPerpareStatement(sql);
		DbUtil.setPrepareStatementString(1, userId);
		DbUtil.queryPrepareStatement();
		ResultSet rs = DbUtil.getResultSet();
		try {
			while (rs.next()) {
				Record record = new Record();
				record.setMark(rs.getString("mark"));
				record.setMarksource(rs.getString("marksource"));
				record.setTime(rs.getString("time"));
				recordList.add(record);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DbUtil.close();
		return recordList;
	}
}
